#######################################
##     Replicating Tables 3-5        ##
##       Financial Scandals          ##
##   in Hamel and Miller (2018)      ##
#######################################

install.packages(c("Matching", "rgenoud"))
library(Matching)
library(rgenoud)

scandal <- read.csv("scandal_dta.csv")

## All Financial Scandals

financial_scandal <- subset(scandal, financial == 1)

attach(financial_scandal)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_financial <- GenMatch(Tr = scandal_id, X = covariates_match, 
                        BalanceMatrix = covariates_balance, M = 3, 
                        pop.size = 12500, max.generations = 10, wait.generations = 5, 
                        hard.generation.limit = FALSE, int.seed = 703, unif.seed = 507, 
                        fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                        replace = TRUE, ties = FALSE)

effect_financial_vote <- Match(Y = votepct, Tr = scandal_id, 
                           X = covariates_match, M = 3, 
                           exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                           replace = TRUE, ties = FALSE, version = "fast", 
                           Weight.matrix = match_financial)
summary(effect_financial_vote)

effect_financial_win <- Match(Y = win, Tr = scandal_id, 
                               X = covariates_match, M = 3, 
                               exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                               replace = TRUE, ties = FALSE, version = "fast", 
                               Weight.matrix = match_financial,
                              match.out = effect_financial_vote)
summary(effect_financial_win)

effect_financial_money <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                            X = covariates_match, M = 3, 
                            exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                            replace = TRUE, ties = FALSE, version = "fast", 
                            Weight.matrix = match_financial, 
                            match.out = effect_financial_vote)
summary(effect_financial_money)

balance_financial <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                dwnom1 + seniority + les + dpres + 
                                logged_pre_sum_infl + chall_quality, 
                              match.out = effect_financial_vote,
                              nboots = 5000)

detach(financial_scandal)

## Covered Financial Scandals

financial_scandal_covered <- subset(scandal, financial == 1 & covered == 1)

attach(financial_scandal_covered)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_financial_covered <- GenMatch(Tr = scandal_id, X = covariates_match, 
                                BalanceMatrix = covariates_balance, M = 3, 
                                pop.size = 12500, max.generations = 10, wait.generations = 5, 
                                hard.generation.limit = FALSE, int.seed = 803, unif.seed = 962, 
                                fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                replace = TRUE, ties = FALSE)

effect_financial_vote_covered <- Match(Y = votepct, Tr = scandal_id, 
                                   X = covariates_match, M = 3, 
                                   exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                   replace = TRUE, ties = FALSE, version = "fast", 
                                   Weight.matrix = match_financial_covered)
summary(effect_financial_vote_covered)

effect_financial_win_covered <- Match(Y = win, Tr = scandal_id, 
                                        X = covariates_match, M = 3, 
                                        exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                        replace = TRUE, ties = FALSE, version = "fast", 
                                        Weight.matrix = match_financial_covered, 
                                        match.out = effect_financial_vote_covered)
summary(effect_financial_win_covered)

effect_financial_money_covered <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                    X = covariates_match, M = 3, 
                                    exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                    replace = TRUE, ties = FALSE, version = "fast", 
                                    Weight.matrix = match_financial_covered, 
                                    match.out = effect_financial_vote_covered)
summary(effect_financial_money_covered)



balance_financial_covered <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                        dwnom1 + seniority + les + dpres + 
                                        logged_pre_sum_infl + chall_quality, 
                                      match.out = effect_financial_vote_covered,
                                      nboots = 5000)
detach(financial_scandal_covered)


## Non-Covered Financial Scandals

financial_scandal_noncovered <- subset(scandal, financial == 1 & covered == 0)

attach(financial_scandal_noncovered)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_financial_noncovered <- GenMatch(Tr = scandal_id, X = covariates_match, 
                                   BalanceMatrix = covariates_balance, M = 3, 
                                   pop.size = 12500, max.generations = 10, wait.generations = 5, 
                                   hard.generation.limit = FALSE, int.seed = 639, unif.seed = 193, 
                                   fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                   replace = TRUE, ties = FALSE)

effect_financial_vote_noncovered <- Match(Y = votepct, Tr = scandal_id, 
                                      X = covariates_match, M = 3, 
                                      exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                      replace = TRUE, ties = FALSE, version = "fast", 
                                      Weight.matrix = match_financial_noncovered)
summary(effect_financial_vote_noncovered)

effect_financial_win_noncovered <- Match(Y = win, Tr = scandal_id, 
                                           X = covariates_match, M = 3, 
                                           exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                           replace = TRUE, ties = FALSE, version = "fast", 
                                           Weight.matrix = match_financial_noncovered, 
                                           match.out = effect_financial_vote_noncovered)
summary(effect_financial_win_noncovered)

effect_financial_money_noncovered <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                       X = covariates_match, M = 3, 
                                       exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                       replace = TRUE, ties = FALSE, version = "fast", 
                                       Weight.matrix = match_financial_noncovered, 
                                       match.out = effect_financial_vote_noncovered)
summary(effect_financial_money_noncovered)




balance_financial_noncovered <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                           dwnom1 + seniority + les + dpres + 
                                           logged_pre_sum_infl + chall_quality, 
                                         match.out = effect_financial_vote_noncovered,
                                         nboots = 5000)

detach(financial_scandal_noncovered)
